define(['jquery', 'bootstrap', 'backend', 'table', 'form'],
    function ($, undefined, Backend, Table, Form) {

        //通用搜索部分的二级联动表单搜索 通过超市input值的改变
        // 进行对对应仓库下来表单的重新赋值
        $(document).on("change", "#supermarket_id_text", function () {
            $.post("warehouse/index", {
                //搜索条件，上一个selectpage选择完后传过来的id作为此次搜索的条件
                custom: {supermarket_id: $('#supermarket_id').val()},
                keyField: 'id',
                showField: 'name',
                //上面两个参数对应的都是表的字段名
            }, function (result) {
                //刷新数据的关键，动态更改数据源
                $("#warehouse_id").selectPageData(result.list);
            });
        });
        var Controller = {
            index: function () {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: 'product/index' + location.search,
                        add_url: 'product/add',
                        edit_url: 'product/edit',
                        del_url: 'product/del',
                        multi_url: 'product/multi',
                        import_url: 'product/import',
                        table: 'product',
                    }
                });

                var table = $("#table");
                //外键关联查询处理 通用搜索部分 表单需要全部初始化 否则后期赋值报错
                table.on('post-common-search.bs.table', function (event, table) {
                    var form = $('form', table.$commonsearch);
                    $("input[name='supermarket_id']", form)
                        .addClass('selectpage')
                        .data("source", "Supermarkets/index")
                        .data('primaryKey', "id")
                        .data('select-only', true);
                    if ($(".selectpage", form).size() > 0) {
                        Form.events.selectpage(form);
                    }
                    $("input[name='warehouse_id']", form)
                        .addClass('selectpage')
                        .data("source", "Warehouse/index")
                        .data('primaryKey', "id")
                        .data('select-only', true);
                    if ($(".selectpage", form).size() > 0) {
                        Form.events.selectpage(form);
                    }
                });

                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    pk: 'id',
                    sortName: 'id',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id'), operate: false},
                            {field: 'name', title: __('Name'), operate: 'LIKE'},
                            {
                                field: 'avatar',
                                title: __('Avatar'),
                                operate: false,
                                events: Table.api.events.image,
                                formatter: Table.api.formatter.image
                            },
                            {field: 'description', title: __('Description'), operate: 'LIKE'},
                            {field: 'price', title: __('Price')},
                            {field: 'generate_date', title: __('Generate_date')},
                            {field: 'shel_flife', title: __('Shel_flife'), operate: 'LIKE'},
                            {field: 'supermarket_id', title: __('Supermarket_id'), visible: false},
                            {field: 'supermarket.name', title: __('Supermarket_id'), operate: false},
                            {field: 'warehouse_id', title: __('Warehouse_id'), visible: false},
                            {field: 'warehouses.name', title: __('Warehouse_id'), operate: false},
                            {field: 'num', title: __('Num')},
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: table,
                                events: Table.api.events.operate,
                                formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });
                // 为表格绑定事件
                Table.api.bindevent(table);
            },
            add: function () {
                //表单二级联动
                $("#c-warehouse_id").data("params", function (obj) {
                    //supermarket_id为发送过去查询的参数id
                    return {custom: {supermarket_id: $("#c-supermarket_id").val()}};
                });
                Controller.api.bindevent();
            },
            edit: function () {
                $("#c-warehouse_id").data("params", function (obj) {
                    return {custom: {supermarket_id: $("#c-supermarket_id").val()}};
                });
                Controller.api.bindevent();
            },
            api: {
                bindevent: function () {
                    Form.api.bindevent($("form[role=form]"));
                }
            }
        };
        return Controller;
    });
